unit MainWindow;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, AXVLC_1_0_TLB, Forms, Controls,
  Graphics, Dialogs, Spin, StdCtrls, FileCtrl, LCLIntf, variants, Windows;

type

  { TSLLibMainWindow }

  TSLLibMainWindow = class(TForm)
    VLCPlayer: TAxcVLCPlugin2;
    BtnSelectFolder: TButton;
    BtnDelete: TButton;
    BtnRename: TButton;
    BtnPlayMovie: TButton;
    btnUpdatePkg: TButton;
    chkRenPlayNext: TCheckBox;
    chkWarn: TCheckBox;
    chkDelPlayNext: TCheckBox;
    ComboBox1: TComboBox;
    Edit1: TEdit;
    FileListBox1: TFileListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SelectDirectoryDialog1: TSelectDirectoryDialog;
    SpinEdit1: TSpinEdit;
    SpinEdit2: TSpinEdit;
    procedure BtnDeleteClick(Sender: TObject);
    procedure BtnPlayMovieClick(Sender: TObject);
    procedure BtnRenameClick(Sender: TObject);
    procedure BtnSelectFolderClick(Sender: TObject);
    procedure btnUpdatePkgClick(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: char);
    procedure FormCreate(Sender: TObject);
    function IsVideoFile(fname: String): Boolean;
    procedure SpinEdit1Click(Sender: TObject);
    procedure SpinEdit2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  SLLibMainWindow: TSLLibMainWindow;

const
  IllegalChar: array [0..8] of char = (
    '\', '/', ':', '*', '?', '"', '<', '>', '|' );

  BibleBooks: array[0..65] of string = (
    'Genesis',
    'Exodus',
    'Leviticus',
    'Numbers',
    'Deuteronomy',
    'Joshua',
    'Judges',
    'Ruth',
    '1 Samuel',
    '2 Samuel',
    '1 Kings',
    '2 Kings',
    '1 Chronicles',
    '2 Chronicles',
    'Ezra',
    'Nehemiah',
    'Esther',
    'Job',
    'Psalms',
    'Proverbs',
    'Ecclesiastes',
    'Song of Solomon',
    'Isaiah',
    'Jeremiah',
    'Lamentations',
    'Ezekiel',
    'Daniel',
    'Hosea',
    'Joel',
    'Amos',
    'Obadiah',
    'Jonah',
    'Micah',
    'Nahum',
    'Habakkuk',
    'Zephaniah',
    'Haggai',
    'Zechariah',
    'Malachi',
    'Matthew',
    'Mark',
    'Luke',
    'John',
    'Acts',
    'Romans',
    '1 Corinthians',
    '2 Corinthians',
    'Galatians',
    'Ephesians',
    'Philippians',
    'Colossians',
    '1 Thessalonians',
    '2 Thessalonians',
    '1 Timothy',
    '2 Timothy',
    'Titus',
    'Philemon',
    'Hebrews',
    'James',
    '1 Peter',
    '2 Peter',
    '1 John',
    '2 John',
    '3 John',
    'Jude',
    'Revelation');

implementation

{$R *.lfm}

{ TSLLibMainWindow }

procedure TSLLibMainWindow.FormCreate(Sender: TObject);
begin
  Label5.Caption := FileListBox1.Directory;
  SLLibMainWindow.Left := (Screen.Width - SLLibMainWindow.Width) div 2;
  SLLibMainWindow.Top := (Screen.Height - SLLibMainWindow.Height) div 2;
end;

function TSLLibMainWindow.IsVideoFile(fname: String): Boolean;
begin
  if CompareFileExt(fname, 'vob', False) = 0 then begin
    Result := True;
    Exit;
  end;
  if CompareFileExt(fname, 'mpg', False) = 0 then begin
    Result := True;
    Exit;
  end;
  if CompareFileExt(fname, 'm4v', False) = 0 then begin
    Result := True;
    Exit;
  end;
  if CompareFileExt(fname, 'mp4', False) = 0 then begin
    Result := True;
    Exit;
  end;

  Result := False;
end;

procedure TSLLibMainWindow.SpinEdit1Click(Sender: TObject);
begin
  //if PasLibVlcMediaList1.IsPlay then PasLibVlcMediaList1.Pause;
end;

procedure TSLLibMainWindow.SpinEdit2Click(Sender: TObject);
begin
  //if PasLibVlcMediaList1.IsPlay then PasLibVlcMediaList1.Pause;
end;

procedure TSLLibMainWindow.BtnSelectFolderClick(Sender: TObject);
begin
  if SelectDirectoryDialog1.Execute then begin
    FileListBox1.Directory := SelectDirectoryDialog1.FileName;
    Label5.Caption := SelectDirectoryDialog1.FileName;
  end;
end;

procedure TSLLibMainWindow.btnUpdatePkgClick(Sender: TObject);
begin
  { TODO 3 : Make new form for Build Update Package function }
end;

procedure TSLLibMainWindow.ComboBox1Click(Sender: TObject);
begin
  //if PasLibVlcMediaList1.IsPlay then PasLibVlcMediaList1.Pause;
end;

procedure TSLLibMainWindow.Edit1Change(Sender: TObject);
begin
  //if PasLibVlcMediaList1.IsPlay then PasLibVlcMediaList1.Pause;
end;

procedure TSLLibMainWindow.Edit1KeyPress(Sender: TObject; var Key: char);
var
   i: Integer;
begin
   for i := 0 to 8 do begin
       if IllegalChar[i] = Key then begin
         Edit1.ShowHint := True;
         Edit1.Hint := 'A filename can''t contain the characters \ / : * ? " < > |';
         Key := #0;
       end;
   end;
end;

procedure TSLLibMainWindow.BtnPlayMovieClick(Sender: TObject);
var
  i, itemNum: Integer;
  fname: String;
begin
  i := FileListBox1.ItemIndex;
  if i > -1 then begin
    fname := FileListBox1.Directory + PathDelim + FileListBox1.Items[i];

    if IsVideoFile(fname) then begin
      itemNum := VLCPLayer.OleServer.playlist.add(utf8decode('file:///' + fname), '', vararrayof([':aspect-ratio=4:3']));
      VLCPLayer.OleServer.playlist.playItem(itemNum);
    end
    else ShowMessage('Not a Video File');

  end
  else ShowMessage('Please select a file');
end;

procedure TSLLibMainWindow.BtnRenameClick(Sender: TObject);
var
  i, BookNum: Integer;
  fname, NewName, BibleBook, BibleChapter, BibleVerse, Source: String;
  TF: Boolean;
begin
  i := FileListBox1.ItemIndex;
  if i > -1 then begin
    SetCurrentDirUTF8(FileListBox1.Directory);
    fname := FileListBox1.Items[i];

    if IsVideoFile(fname) then begin
       BookNum := ComboBox1.ItemIndex;
       BibleBook := Format('%.2d', [ComboBox1.ItemIndex + 1]);
       BibleChapter := Format('%.3d', [SpinEdit1.Value]);
       BibleVerse := Format('%.3d', [SpinEdit2.Value]);
       Source := Trim(Edit1.Text);

       if Source = '' then begin
          ShowMessage('Please enter a source for this scripture.');
          Exit;
       end;

       //Build the filename
       NewName := BibleBook + '.' + BibleChapter + '.' + BibleVerse + ' ' +
         BibleBooks[BookNum] + ' ' + IntToStr(SpinEdit1.Value) + '-' +
         IntToStr(SpinEdit2.Value) + '.' + Source + '.mpg';

       //Rename it
       if VLCPlayer.OleServer.playlist.Get_isPlaying then VLCPLayer.OleServer.playlist.stop;
       TF := RenameFileUTF8(fname, NewName);
       if TF = False then ShowMessage('Unable to Rename File. Close media player and try again.');

       //Update list and keep selection
       FileListBox1.UpdateFileList;
       Inc(i);
       if FileListBox1.Items.Count > i then FileListBox1.ItemIndex := i;
       if chkRenPlayNext.Checked then BtnPlayMovieClick(Sender);
    end
    else ShowMessage('Not a Video File');

  end
  else ShowMessage('Please select a file');
end;

procedure TSLLibMainWindow.BtnDeleteClick(Sender: TObject);
var
  i: Integer;
  fname, DeleteDir: String;
  boxResult: TModalResult;
begin
  i := FileListBox1.ItemIndex;
  if i > -1 then begin  {Is there something really selected?}

    {Protect the user from himself}
    if chkWarn.Checked then begin;
      boxResult := QuestionDlg('Delete', 'Are you sure you want to delete this file?',
                          mtConfirmation, [mrYes, mrNo], 0);
      if boxResult <> mrYes then exit;
    end;

    {Proceed with deletion}
    SetCurrentDirUTF8(FileListBox1.Directory);
    fname := FileListBox1.Items[i];

    //Stop the player or the OS won't let you delete
    if VLCPlayer.OleServer.playlist.Get_isPlaying then VLCPLayer.OleServer.playlist.stop;

    if IsVideoFile(fname) then begin
      DeleteDir := FileListBox1.Directory + PathDelim + 'Deleted';
      if not DirectoryExistsUTF8(DeleteDir) then
        CreateDirUTF8(DeleteDir);

      if not MoveFile(LPCSTR(fname), LPCSTR(DeleteDir + PathDelim + fname)) then
        ShowMessage('Error Moving File.');
    end
    else ShowMessage('Not a video file');

    FileListBox1.UpdateFileList;
    if FileListBox1.Items.Count > i then FileListBox1.ItemIndex := i;
    if chkDelPlayNext.Checked then BtnPlayMovieClick(Sender);
  end
  else begin
    ShowMessage('Please select a file');
  end;
end;

end.

